#include <iostream>
#include <assert.h>
#include <xwritelogexport.h>
#include <xinifile.h>
#include "xinfomatscreenmodeparser.h"

const char k_ini_sec[] = "";
const char k_key_screenMode[] = "ScreenMode";
const char k_ini_bootconf[]= "/usr/infomat/boot.conf";

XInfomatScreenModeParser::XInfomatScreenModeParser()
	:m_result(INFOMAT_RESULT_OK)
{
}

XInfomatScreenModeParser::~XInfomatScreenModeParser()
{
}

void XInfomatScreenModeParser::getResource(QByteArray in_datagram)
{
	assert (0!=in_datagram.size());
	char *data  = new char[in_datagram.size()];
    memcpy(data, in_datagram.data(), in_datagram.size());
	assert (INFOMAT_CONFIG_PROTOCOL == *data);

	STInfomatScreenMode *tmpdata = reinterpret_cast<STInfomatScreenMode*>(data);
	memcpy(&m_stScreenMode, tmpdata, sizeof(m_stScreenMode));
	tmpdata = NULL;
	if (NULL!=data) {
		delete data;
		data = NULL;
	}
	
	this->debugOutput();

	switch (m_stScreenMode.head.command) 
	{
	case INFOMAT_COMMAND_READ:
		read();
		break;
	case INFOMAT_COMMAND_WRITE:
		write();
		break;
	default:
		DEBUG("No such command");
		exit(0);   // debug only
	}
	return;
}

/* ****************************************************************
 * read the value from ini file
 * ****************************************************************/
void XInfomatScreenModeParser::read()
{
	XIniFile iniFile;
	assert (0==iniFile.open(k_ini_bootconf));
	int value;
	if (0!=iniFile.readInt(k_ini_sec, k_key_screenMode, value)) {
		LOG_E("Read screen Mode value failed");
		m_result = INFOMAT_RESULT_ERR;
	}
	iniFile.close();
	DEBUG("screen mode [%d]", value);
	m_stScreenMode.screenMode = value;
	return;
}

/* ***************************************************************
 * write the value into boot.conf
 * ***************************************************************/
void XInfomatScreenModeParser::write()
{
	XIniFile iniFile;
	assert(0==iniFile.open(k_ini_bootconf,EIniReadWrite));
	if(0!=iniFile.writeInt(k_ini_sec,k_key_screenMode, int(m_stScreenMode.screenMode))) {
		LOG_E("Write screen mode failed");
		m_result = INFOMAT_RESULT_ERR;
	}
	iniFile.flush();
	iniFile.close();
	return;
}

/* ****************************************************************
 * send out the datagram
 * *****************************************************************/
QByteArray XInfomatScreenModeParser::getOutDatagram()
{
    m_stScreenMode.head.result = m_result;
    QByteArray outdata = QByteArray (reinterpret_cast<char*> (&m_stScreenMode), sizeof(STInfomatScreenMode));
	return outdata;
}

/* ***************************************************************
 * [#] debug output socket infomation
 * ***************************************************************/
void XInfomatScreenModeParser::debugOutput()
{
	LOG_T("ScreenMode Socket Checking ...");
	LOG_T("protocol Id: [%02x]", m_stScreenMode.head.protocol);
	LOG_T("type:[%02d]", m_stScreenMode.head.type);
	LOG_T("size:[%02d]", m_stScreenMode.head.size);
	LOG_T("result:[%x]",   m_stScreenMode.head.result);
	LOG_T("command:[%02d]", m_stScreenMode.head.command);
	LOG_T("screen mode:[%02d]", m_stScreenMode.screenMode);
}
